package anshe.object
{
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import org.dstart.csuper.SuperSprite;

	public class ExScroll extends SuperSprite
	{
		public static const UP_CLICK:String		= "upClick";	/** 点击 向上 按钮 **/
		public static const DOWN_CLICK:String	= "downClick";	/** 点击 向下 按钮 **/
		public static const CHANGE_MOVE:String	= "changeMove";	/** 修改当前对象位置 **/
		
		public var bgSprite:Sprite;			/** 底部热区 **/
		public var upButton:SimpleButton;	/** 上方向按钮 **/
		public var downButton:SimpleButton;	/** 下方向按钮 **/
		public var dragButton:Sprite;		/** 可拖动按钮 **/
		
		/** 传入参数 hh 为滑动条高度，0 为默认的原始大小 **/
		public function ExScroll(hh:Number = 0)
		{
			if(hh > 0)
			{
				ChangeHeight(hh);
			}
			
			this.bgSprite.addEventListener(MouseEvent.CLICK, onBgClick);
			this.upButton.addEventListener(MouseEvent.CLICK, onUpClick);
			this.downButton.addEventListener(MouseEvent.CLICK, onDownClick);
			this.dragButton.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
			this.dragButton.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
		}
		
		/** 修改滑动条高度 **/
		public function ChangeHeight(hh:Number):void
		{
			bgSprite.height = hh;
			upButton.y = 0;
			dragButton.y = upButton.y + upButton.height;
			downButton.y = bgSprite.height - downButton.height;
		}
		
		/** 设置百分比 **/
		public function set percent(num:Number):void
		{
			var maxHeight:Number = this.bgSprite.height - this.upButton.height - this.downButton.height - this.dragButton.height;
			var yTop:Number = this.upButton.y + this.upButton.height;
			
			num = (num < 0 ) ? 0 : (num > 100) ? 100 : Math.floor(num);
			
			this.dragButton.y = yTop + maxHeight * num/100;
		}
		
		/** 取得百分比 （按百分比值返回） **/
		public function get percent():Number
		{
			var maxHeight:Number = this.bgSprite.height - this.upButton.height - this.downButton.height - this.dragButton.height;
			var yTop:Number = this.upButton.y + this.upButton.height;
			
			return Math.ceil((this.dragButton.y - yTop) / maxHeight * 100);
		}
		
		/** 鼠标在长条区块上点击，根据鼠标点位置计划当前滑动百分比 **/
		private function onBgClick(e:MouseEvent):void
		{
			this.dragButton.y = this.mouseY - this.dragButton.height/2;
			
			if(this.dragButton.y < this.upButton.y + this.upButton.height)
			{
				this.dragButton.y = this.upButton.y + this.upButton.height;
			}
			else if(this.dragButton.y > this.downButton.y - this.dragButton.height)
			{
				this.dragButton.y = this.downButton.y - this.dragButton.height;
			}
			
			this.dispatchEvent(new Event(ExScroll.CHANGE_MOVE));
		}
		
		/** 点击 向上 按钮 **/
		private function onUpClick(e:MouseEvent):void
		{
			this.dispatchEvent(new Event(UP_CLICK));
		}
		
		/** 点击 向下 按钮 **/
		private function onDownClick(e:MouseEvent):void
		{
			this.dispatchEvent(new Event(DOWN_CLICK));
		}
		
		/** 开始拖动滑块 **/
		private function onStartDrag(e:MouseEvent):void
		{
			var maxHeight:Number = this.bgSprite.height - this.dragButton.height - this.upButton.height - this.downButton.height;
			var rect:Rectangle = new Rectangle(this.upButton.x, this.upButton.y + this.upButton.height,  0, maxHeight);
					
			this.dragButton.startDrag(false, rect);
			
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onDragMove);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
		}
		
		/** 滑块拖动中 **/
		private function onDragMove(e:MouseEvent):void
		{
			this.dispatchEvent(new Event(CHANGE_MOVE));
		}
		
		/** 停止拖动对象 **/
		private function onStopDrag(e:MouseEvent):void
		{
			this.dragButton.stopDrag();
			
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDragMove);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onStopDrag);
		}
	}
}